/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.ui;

import android.os.Bundle;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.demo.widget.BaseFragmentActivity;
import com.mol.market.ui_v4.PagerAdapter;

/**
 * 分类列表页(Tab + List)
 *
 * @author Chaos
 * @Description 入口
 * <br>
 * (游戏/应用)--分类--点击某一项分类  --> {@link CategoryListFragment}
 * @date 2013-1-23
 */
public class TabFragmentActivity extends BaseFragmentActivity{

    protected void setupTabs(PagerAdapter adapter, ActionBar bar) {

        Tab tab1 = bar.newTab().setText(R.string.sort_tab_pop);
        Tab tab2 = bar.newTab().setText(R.string.sort_tab_new);

        Bundle args = new Bundle();

        if (getIntent() != null) {
            args.putString(Constants.EXTRA_CATEGORY_ID, getIntent().getStringExtra(Constants.EXTRA_CATEGORY_ID));

            //set title
            bar.setTitle(getIntent().getStringExtra(KeyStore.KEY_TITLE));
            bar.setDisplayHomeAsUpEnabled(true);
        }

        Bundle left = (Bundle) args.clone();
        left.putBoolean("left", true);

        Bundle right = (Bundle) args.clone();
        right.putBoolean("left", false);

        adapter.addTab(tab1, CategoryListFragment.class, tab1.getText(), left);
        adapter.addTab(tab2, CategoryListFragment.class, tab2.getText(), right);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_home);
        initTabs();
    }
    
    private void initTabs(){
        ActionBar bar   = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        
        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        
        PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), this, getApplicationContext(), pager, bar);
        
        setupTabs( adapter, bar);

        pager.setAdapter(adapter);
    }
    

}
